package edu.xjtu.jih.lost;

import edu.xjtu.jih.common.Result;
import edu.xjtu.jih.common.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet({
        "/lost/lostList",
        "/lost/lostDoAdd",
        "/lost/lostDoUpd",
        "/lost/lostDoDel"
})
public class LostAPI extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LostService lostService = new LostService();
        request.setCharacterEncoding("UTF-8");
        String path = request.getServletPath();

        if("/lost/lostDoAdd".equals(path)){

            LostDto dto = Utils.getBeanFromRequest(LostDto.class,request);
            //teaService.addTeacher(dto);
            lostService.addLostitem(dto);

            Utils.outJson(response, Result.success());
        }

        else if("/lost/lostDoUpd".equals(path)){

            try {
                LostDto dto = Utils.getBeanFromRequest(LostDto.class,request);
                //teaService.modifyTeacher(dto);
                lostService.modifyLostitem(dto);

                Utils.outJson(response, Result.success());
            } catch (IOException e) {
                e.printStackTrace();
                throw e;
            }

        }

        else if("/lost/lostDoDel".equals(path)){

            try {
                LostDto dto = Utils.getBeanFromRequest(LostDto.class,request);
                //System.out.println("dto:"+dto);
                lostService.removeLostitem(dto);

                Utils.outJson(response, Result.success());
            } catch (Exception e) {
                e.printStackTrace();
                Utils.outJson(response, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收客户端（浏览器）传来的参数,并将参数封装为Java对象
        request.setCharacterEncoding("UTF-8");
        QuerylostDto queryDto = new QuerylostDto();

        try {
            queryDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
        } catch (Exception e) {}

        try {
            queryDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        } catch (Exception e) {}

        queryDto.setLf_id(request.getParameter("lf_id"));
        queryDto.setLf_name(request.getParameter("lf_name"));
        try {
            queryDto.setLf_kind(Integer.parseInt(request.getParameter("lf_kind")));
        } catch (Exception e) {}
        
        queryDto.setLf_userid(request.getParameter("lf_userid"));

        queryDto.setLf_time(request.getParameter("lf_time"));
        queryDto.setLf_start_time(request.getParameter("lf_start_time"));
        queryDto.setLf_end_time(request.getParameter("lf_end_time"));
        //.setLf_iden(Integer.parseInt(request.getParameter("lf_iden")));
        //创建业务模型对象（服务对象/Service对象），通过业务对象执行业务方法
        LostService lostService = new LostService();
        Map<String,Object> page =  lostService.getLostList(queryDto);

       // Map<String,Object> page0 =  lostService.getLostList0("2196113473",queryDto);     //应该加入当前用户的id
        //通过视图生成json
        Utils.outResult(response, Result.success(page));

    }

}
